<template>
	<view class="u-form">
		<slot></slot>
	</view>
</template>

<script setup lang="ts">
	import { tip } from '@/utils'
	
	const validate = (rules:any, form:any) => {
		return new Promise((resolve,reject) => {
			let res = true
			if(!rules) reject('请输入规则对象！')
			if(!form) reject('请输入表单对象!')
			for(let i in rules) {
				for(let j in rules[i] ) {
					if(rules[i][j].required === true && !form[i] && form[i] != '0') {
						tip.showToast(rules[i][j].message)
						res = false
					} else if(rules[i][j].max && form[i] > rules[i][j].max) {
						tip.showToast(rules[i][j].message)
						res = false
					} else if(rules[i][j].pattern) {
						if(!rules[i][j].pattern.test(form[i])) {
							tip.showToast(rules[i][j].message)
							res = false
						}
					}
				}
			}
			if(res === true) {
				resolve(true)
			}	
		})
	}
	
	defineExpose({
	  validate
	})
	
</script>

<style lang="scss" scoped>
	.u-form {
		background-color: $uni-bg-card-color;
		padding:0 40rpx;
	}
</style>